package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import Connection.GenRetriever;


public class DodajPodmiotInputFrame extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -270652332979988660L;

	JTextField text1;
	JComboBox listaPrac;
	JButton button1, button2;
	GenRetriever listaPracR;
	DefaultComboBoxModel listaPracM;
	ArrayList<String> choiceStr1;
	String u, n;

	public DodajPodmiotInputFrame() throws HeadlessException {
		super("Dodaj podmiot");
	}
	
	public void start()
	
    {
		JLabel label1 = new JLabel("Nazwa podmiotu: ");
	    JLabel label2 = new JLabel("Pracownik ");

	    text1 = new JTextField(20);
	    
	    choiceStr1 = new ArrayList<String>();
	    listaPracR = new GenRetriever();
	    choiceStr1 = listaPracR.get("Nazwisko +' '+ Imie as 'NAZWA'", "PRACOWNIK"); 
	    listaPracM = new javax.swing.DefaultComboBoxModel(choiceStr1.toArray());

		listaPrac = new JComboBox(choiceStr1.toArray());
		listaPracM.addElement(" ");
		listaPrac.setModel(listaPracM);
		listaPrac.setSelectedIndex((listaPracM.getSize())-1);
		listaPrac.addActionListener(this);

	    button1 = new JButton("Dodaj");
	    button2 = new JButton("Zamknij");

	    this.add(label1, BorderLayout.WEST);
	    this.add(text1, BorderLayout.EAST);
	    this.add(label2, BorderLayout.WEST);
	    this.add(listaPrac, BorderLayout.EAST);

	    this.add(button1);
	    this.add(button2);
	    
	    button1.addActionListener(this );
	    button2.addActionListener(this );
		
	    setLayout(new FlowLayout());
	    setSize(250,150);
	    setVisible(true);
	    Toolkit kit = Toolkit.getDefaultToolkit();
	    Dimension screenSize = kit.getScreenSize();
	    setLocation((screenSize.width-250)/2, (screenSize.height-150)/2);
	    setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "Zamknij"){
			this.dispose();
		}
		else if(e.getActionCommand() == "Dodaj"){
			try {
			     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			     Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ABSI", "Admin","Admin");
			     CallableStatement statement = conn.prepareCall("EXEC DodajPodmiot ?, ?"); 
			     	
			        n = (String)listaPrac.getSelectedItem();
			        u = text1.getText();
			     
			     statement.setString(1,n);
			     statement.setString(2,u);
			     statement.execute();

			     conn.close();
			  } catch (Exception ex) {
			     ex.printStackTrace();
			  }
		}
		text1.setText((String)listaPrac.getSelectedItem());
		
	}

}
